package com.ayoka.parenting.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ViewAnimator;

import com.ayoka.parenting.R;
import com.jincp.common.utils.UIUtils;

/**
 * Created by jinchunping on 2018/4/25.
 */
@SuppressLint("NewApi")
public class MeditationBreatheView extends View {

    private int size;
    private float ringWidth = 10;
    private float ringRadio = 200;
    private int ringColor = Color.WHITE;
    private int pointColor = Color.WHITE;
    private float pointRadio = 10;
    private Paint ringPaint;

    public MeditationBreatheView(Context context) {
        super(context);
    }

    public MeditationBreatheView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MeditationBreatheView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        /**
         * 获得属性
         */
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MeditationBreatheView);
        if (typedArray != null) {
            ringWidth = typedArray.getDimensionPixelSize(R.styleable.MeditationBreatheView_ring_width, 10);
            ringRadio = typedArray.getDimensionPixelSize(R.styleable.MeditationBreatheView_ring_radio, 200);
            ringColor = typedArray.getColor(R.styleable.MeditationBreatheView_ring_color, Color.WHITE);
            pointColor = typedArray.getColor(R.styleable.MeditationBreatheView_point_color, Color.WHITE);
            pointRadio = typedArray.getDimension(R.styleable.MeditationBreatheView_point_radio, 10);
        }
        typedArray.recycle();
        initPaint();
    }

    private void initPaint() {
        ringPaint = new Paint();
        ringPaint.setColor(ringColor);
        ringPaint.setStyle(Paint.Style.STROKE);
        ringPaint.setAntiAlias(true);
        ringPaint.setStrokeWidth(ringWidth);
    }

    @SuppressWarnings("unused")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        size = Math.min(width, height);
        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(size / 2, size / 2);
        drawCircle(canvas);
        drawPoint(canvas);
    }

    private void drawPoint(Canvas canvas) {
        canvas.save();
        ringPaint.setColor(pointColor);
        ringPaint.setStyle(Paint.Style.FILL);
        ringPaint.setStrokeWidth(pointRadio);
        ringPaint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_ATOP));
        for (int i = 0; i < 360; i += 90) {
            float translateX = (float) (Math.sin(Math.PI * i / 180) * ringRadio);
            float translateY = (float) (Math.cos(Math.PI * i / 180) * ringRadio);
            canvas.drawCircle(translateX, -translateY, pointRadio, ringPaint);
            canvas.rotate(i);
        }
        canvas.restore();
    }

    private void drawCircle(Canvas canvas) {
        // 绘制圆环
        canvas.drawCircle(0, 0, ringRadio, ringPaint);
    }

    public void startBreathe() {
        AnimatorSet animatorSet = new AnimatorSet();  //组合动画
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(this, "scaleX", 1f, 1.05f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(this, "scaleY", 1f, 1.05f);
        scaleX.setRepeatMode(ValueAnimator.REVERSE);
        scaleX.setRepeatCount(ValueAnimator.INFINITE);
        scaleY.setRepeatMode(ValueAnimator.REVERSE);
        scaleY.setRepeatCount(ValueAnimator.INFINITE);
        animatorSet.setDuration(3000);  //动画时间
        animatorSet.play(scaleX).with(scaleY);  //同时执行
        animatorSet.start();  //启动动画
    }

}
